package com.mavenq.fly.test;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ToString
public class EmployeeSalary {
    Long id;
    String employeeName;
    String salary;
    String salaryAfterTax;
}

@Data
class TaxTable {
    // id
    private Long id;
    // 薪资范围 0-1000  1000-10000  10000-20000 20000-
    private String salaryRange;
    // 税率
    private Double taxRate;
    // 排序字段 薪资越高 order越大
    private int order;
}

class SalaryCalculator {
    public static void calSalaryAfterTax(List<EmployeeSalary> salaryList,
                                         List<TaxTable> taxTable) {

        // 请填写计算方法，将税后工资计算结果放到salaryAfterTax字段中
        if (salaryList != null && salaryList.size() > 0) {
            salaryList.stream().forEach(employeeSalary -> {
                Long salary = Long.valueOf(employeeSalary.getSalary());
                employeeSalary.setSalaryAfterTax(calculatePostTaxValue(salary, taxTable));
            });
        }
    }

    private static String calculatePostTaxValue(Long salary, List<TaxTable> taxTable) {
        // 总税额
        Double taxSum = 0d;
        if (taxTable != null && taxTable.size() > 0) {
            // 降序排，遍历后按降序顺序取
            List<TaxTable> sortedTaxTable = taxTable.stream().sorted(Comparator.comparingLong(TaxTable::getOrder)
                    .reversed()).collect(Collectors.toList());
            boolean flag = false;
            for (TaxTable table : sortedTaxTable) {
                String[] split = table.getSalaryRange().split("-");
                Long begin = Long.valueOf(split[0]);
                Long end = null;
                if (split.length > 1) {
                    end = Long.valueOf(split[1]);
                }
                if (salary < begin) {
                    continue;
                }
                if (end == null) {
                    // 匹配最后一个范围
                    taxSum += ((salary - begin) * table.getTaxRate());
                    flag = true;
                } else if ((salary > begin && salary <= end) || flag) {
                    if (flag) {
                        taxSum += ((end - begin) * table.getTaxRate());
                    } else {
                        taxSum += ((salary - begin) * table.getTaxRate());
                        flag = true;
                    }
                }
            }
            return String.valueOf(salary - taxSum);
        }
        return null;
    }

    public static void main(String[] args) {
        List<EmployeeSalary> salaryList = new ArrayList<EmployeeSalary>();
        EmployeeSalary employeeSalary1 = new EmployeeSalary();
        employeeSalary1.setId(1l);
        employeeSalary1.setEmployeeName("Tony");
        employeeSalary1.setSalary("15000");
        salaryList.add(employeeSalary1);
        EmployeeSalary employeeSalary2 = new EmployeeSalary();
        employeeSalary2.setId(2l);
        employeeSalary2.setEmployeeName("Pronub");
        employeeSalary2.setSalary("21300");
        salaryList.add(employeeSalary2);
        List<TaxTable> taxTable = new ArrayList<TaxTable>();
        TaxTable tableOne = new TaxTable();
        tableOne.setId(1l);
        tableOne.setSalaryRange("0-1000");
        tableOne.setOrder(1);
        tableOne.setTaxRate(0d);
        taxTable.add(tableOne);
        TaxTable tableThree = new TaxTable();
        tableThree.setId(3l);
        tableThree.setSalaryRange("10000-20000");
        tableThree.setTaxRate(0.2);
        tableThree.setOrder(3);
        taxTable.add(tableThree);
        TaxTable tableTwo = new TaxTable();
        tableTwo.setId(2l);
        tableTwo.setSalaryRange("1000-10000");
        tableTwo.setTaxRate(0.1);
        tableTwo.setOrder(2);
        taxTable.add(tableTwo);
        TaxTable tableFour = new TaxTable();
        tableFour.setId(4l);
        tableFour.setSalaryRange("20000-");
        tableFour.setTaxRate(0.3);
        tableFour.setOrder(4);
        taxTable.add(tableFour);
        calSalaryAfterTax(salaryList, taxTable);
        System.out.println(salaryList);
    }
}
